<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
        }
    </style>
</head>

<body>
    <img id="img" src="https://p.ssl.qhimg.com/sdm/242_150_/t0134a21e20151f5854.jpg" style="width:300px !important;"
        alt="">
    <div id="box" style="max-width: 300px !important;"></div>
    <script>
        img.style.setProperty('width', '400px', 'important');
        img.style.height = '400px';

        class ListNode {
            constructor(value) {
                this.val = value;
            }
        }
        // dummy 假头 避免空指针

        class LinkedList {
            constructor(val) {
                this.length = 0;
                this.dummy = new ListNode(val);
                this.tail = this.dummy;
            }
            // 前边添加
            addAtHead() {
                var p = new ListNode();
                p.next = this.dummy.next;
                this.dummy.next = p;
                if (this.dummy == this.tail) {
                    this.tail = p;
                }
                this.length++;
            }
            // 末尾添加
            addAtTail() {
                this.tail.next = new ListNode();
                this.tail = this.tail.next;
                this.length++;

            }
            // 获取前驱节点
            getPrevNode(index) {
                var prev = this.dummy;
                var cur = this.dummy.next;
                for (var i = 0; i < index; i++) {
                    prev = cur;
                    cur = cur.next;
                }
                return prev;
            }
            // 查找节点
            get(index) {
                if (index < 0 || index >= this.length) {
                    return null
                }
                return this.getPrevNode(index).next;
            }
            // 插入指定位置之前

            addAtIndex(idx) {
                if (idx <= 0) {
                    this.addAtHead()
                } else if (idx == this.length) {
                    thisd.addAtTail()
                } else if (idx > this.length) {
                    return
                } else {
                    var prev = this.getPrevNode(idx);
                    var p = new ListNode();
                    p.next = prev.next;
                    prev.next = p;
                    this.length++;
                }
            }

            // 删除节点
            del(idx) {
                if (idx < 0 || idx >= this.length) {
                    return
                }
                var prev = this.getPrevNode(idx);
                // 如果删除的是最后一个节点,需要修改指针
                if (this.tail == prev.next) {
                    this.tail = prev;
                }
                prev.next = prev.next.next;
                this.length--;
            }

            //获取链表长度
            len() {
                var count = 0;
                var tmp = this.dummy;
                while (tmp.next) {
                    tmp = tmp.next;
                    count++
                }
                return count;
            }
        }
    </script>
</body>

</html>